﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace tongxin
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            SearchAndSerialComboBox(serialPort1, comboBox1);
        }
        private void SearchAndSerialComboBox(SerialPort MyPort, ComboBox MyBox)
        {
            string Buffer;
            MyBox.Items.Clear();
            for (int i = 1; i < 20; i++)
            {
                try
                {
                    Buffer = "COM" + i.ToString();
                    MyPort.PortName = Buffer;
                    MyPort.Open();
                    MyBox.Items.Add(Buffer);
                    MyPort.Close();
                }
                catch
                {
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                try
                {
                    serialPort1.Close();
                    button1.Text = "打开串口";
                }
                catch
                {
                }

            }
            else
            {
                try
                {

                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.Open();
                    button1.Text = "关闭串口";
                }
                catch
                {
                    MessageBox.Show("串口打开失败", "提示");
                }
            }
        }
        public static UInt16 CalculateCRC16(UInt16[] data, UInt16 len)
        {
            UInt16 crc = 0xFFFF;
            UInt16 i, j;
            
            for (i = 0; i < len; i++)
            {
                crc ^= data[i];
                for (j = 0; j < 8; j++)
                {
                    
                    if ((crc & 0x0001) != 0)
                    {
                        crc >>= 1;
                        crc ^= 0xA001;
                    }
                    else
                    {
                        crc >>= 1;
                    }
                }
            }

            return crc;
        }
        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            string receivedData = serialPort1.ReadExisting().TrimEnd('\0');
            UInt16 a;

            string[] dataEntries = receivedData.Split(';');
            foreach (string entry in dataEntries)
            {
                string[] tokens = entry.Split('|');
                if (tokens.Length == 3)
                {
                    string data1 = tokens[0];
                    string data2 = tokens[1];
                    string data3 = tokens[2];
                    UInt16[] data= {0,0 };
                    Convert.ToUInt16(data1);
                    Convert.ToUInt16(data2);
                    Convert.ToUInt16(data3);
                    data[0] = Convert.ToUInt16(data1);
                    data[1] = Convert.ToUInt16(data2);
                    a = CalculateCRC16(data,2);

                    if (a == Convert.ToUInt16(data3))
                    {
                        this.Invoke(new EventHandler(delegate
                        {
                            textBox1.AppendText("温度:");
                            textBox1.AppendText(data1);
                            textBox1.AppendText("湿度:");
                            textBox1.AppendText(data2);
                        }));
                    }
                    else
                    { }
                   
                }
            }     
       }


        private void button2_Click(object sender, EventArgs e)
        {
           textBox1.Clear();
        }

       
    }
}


  